import java.awt.* ;
import java.awt.List;
import java.awt.event.* ;
import java.util.*;
import javax.swing.* ;


public class CreateUserDialog extends JFrame 
{
	private JPanel panel;
	
	private JLabel userLabel;
	private JLabel pwdLabel1;
	private JLabel pwdLabel2;
	
	private JTextField userNameTextField;
	private JPasswordField pwdTextField1;
	private JPasswordField pwdTextField2;
	
	private JPanel userpwdPanel;
	private DomainLogic dL; 
	private JButton submitButton;
	
	private PreferencesDialog preferences;
	
	public CreateUserDialog(DomainLogic _dL)
	{
		super( "Create New User" );  
    	panel = new JPanel();
    	panel.setLayout(new GridLayout(3,0));
    				
    	dL = _dL;
		userLabel = new JLabel("Username:");
		userNameTextField = new JTextField();
		userNameTextField.setPreferredSize(new Dimension(100,20));
						
		pwdLabel1 = new JLabel("Password:");
		pwdTextField1 = new JPasswordField();
		pwdTextField1.setMaximumSize(new Dimension(100,20));
		
		pwdLabel2 = new JLabel("Renter Password:");
		pwdTextField2 = new JPasswordField();
		pwdTextField2.setMaximumSize(new Dimension(100,20));
		
		userpwdPanel = new JPanel();
		userpwdPanel.setLayout(new GridLayout(3,2));
		
		userpwdPanel.add(userLabel);
		userpwdPanel.add(userNameTextField);
		userpwdPanel.add(pwdLabel1);
		userpwdPanel.add(pwdTextField1);
		userpwdPanel.add(pwdLabel2);
		userpwdPanel.add(pwdTextField2);
		
		preferences = new PreferencesDialog();
		
		submitButton = new JButton("Submit");
		submitButton.addActionListener(new ActionListener()
		{
		      public void actionPerformed(ActionEvent e)
		      {
		            String username = userNameTextField.getText();
		            String password1 = new String(pwdTextField1.getPassword());
		            String password2 = new String(pwdTextField2.getPassword());
		        		if(username.isEmpty()){
		        			new ErrorMessageDialog("Username was not specified");
		        		}
		            else if(password1.equals("")){
		        			new ErrorMessageDialog("Password was not specified");
		        		}
		            else if(password2.equals("")){
		        			new ErrorMessageDialog("Password verification was not specified");
		        		}
		            else if(!password1.equals(password2)){
		        			new ErrorMessageDialog("Passwords do not match");
		        		}
		            else {
		            	  Preferences prefs = preferences.getPreferences();
		        		  dL.registerUser(username, password1, prefs);
		            }
		      }
		});
		
		panel.add(userpwdPanel);
		panel.add(preferences);
		panel.add(submitButton);
		
	    getContentPane().add(panel);
		setSize( 450, 400 );
		setVisible(true);
	}
}
